package com.demo.action;

import com.demo.domain.Customer;
import com.demo.domain.Department;
import com.demo.service.CustomerService;
import com.demo.service.DepartMentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Version 2019
 * @Author:kele
 * @Date:2020/4/20
 * @Content:
 */
public class DepartMentAction extends ActionSupport implements ModelDriven<Department> {

    private Department department = new Department();
    private DepartMentService departMentService;

    private CustomerService customerService;

    public void setDepartMentService(DepartMentService departMentService) {
        this.departMentService = departMentService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    /*
     * 需要上传文件（流） 需要上传文件名称 1.在action里面成员变量位置定义变量（命名规范） - 一个表示上传文件 -一个表示文件名称
     * 2.生成变量的get和set方法
     */

    // 1.上传文件 变量名称=表单中文件上传项的name值
    private File upload;
    // 2.上传文件名称 表单中文件上传项的name值FileName
    private String uploadFileName;

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String toAddPage() {
        // 1.1查询所有客户，把所有客户list集合传递到页面中显示（放到域对象）
        List<Customer> listCustomer = customerService.find();
        ActionContext.getContext().getValueStack().set("listCustomer", listCustomer);
        return "toAddPage";
    }

    // 2. 添加部门到数据库
    public String addDepartment() throws IOException {

        // 判断是否需要上传文件
        if (upload != null) {
            // 写上传代码
            // 在服务器文件夹里创建文件
            File serverFile = new File("E:\\demoIo\\sshimage" + "/"
                    + uploadFileName);
            // 把上传文件复制到服务器文件夹里
            FileUtils.copyFile(upload, serverFile);
        }
        // 原始方式 实现将cust_id封装到department中的customer对象里
//        String cust_id1 = ServletActionContext.getRequest().getParameter("cust_id");
//        int cust_id=Integer.parseInt(cust_id1);
//
//         Customer c=new Customer();
//         c.setCust_id(cust_id);
//         department.setCustomer(c);
        departMentService.addDepartMent(department);
        return "addDepartMent";
    }

    // 客户列表
    public String listDepartMent() {
        List<Department> list = departMentService.find();
        ServletActionContext.getRequest().setAttribute("list", list);
        return "listDepartMent";
    }

    // 4.到修改联系人页面
    public String showDepartMent() {
        long d_id = department.getD_id();
        Department department = departMentService.findOne(d_id);
        // 需要所有客户的list集合department
        List<Customer> listCustomer = customerService.find();
        ServletActionContext.getRequest().setAttribute("department", department);
        ServletActionContext.getRequest().setAttribute("listCustomer",
                listCustomer);

        return "showDepartMent";
    }

    // 5.修改联系人提交
    public String editsubmit() {
        departMentService.update(department);
        return "editsubmit";
    }

    // 到部门查询页面
    public String toSelectDepartMentPage() {
        List<Customer> list = customerService.find();
        ServletActionContext.getRequest().setAttribute("list", list);
        return "toSelectDepartMentPage";
    }

    // 多条件查询
    public String moreCondition() {
        List<Department> list = departMentService.findMoreCondition(department);
        ServletActionContext.getRequest().setAttribute("list", list);
        return "moreCondition";
    }

    @Override
    public Department getModel() {
        return department;
    }
}
